package com.zany.util.file;


import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;

public class DownloadUtil{
	
	private static String downloadPath = "download";
	
	public static String getDownloadUrl2(String fileName)throws Throwable{
		HttpServletRequest req = ServletActionContext.getRequest();
		String serviceName = req.getServerName();
		String serverPort = "" + req.getServerPort();
		String contextPath = req.getContextPath();
		String downloadUrl = "http://".concat(serviceName).concat(":").concat(serverPort);
		downloadUrl = concatPath(downloadUrl, contextPath);
		downloadUrl = concatPath(downloadUrl, downloadPath);
		downloadUrl = concatPath(downloadUrl, fileName);
		
		return downloadUrl;
	}

	private static String concatPath(String path1, String path2) {
		String path = null;
		path1 = delEndSeparatorChar(path1);
		path2 = delStartSeparatorChar(path2);
		path = path1.concat("/").concat(path2);
		return path;
	}
	
	private static String delStartSeparatorChar(String path) {
		String rstPath = path;
		while ((rstPath.length() > 1) && (rstPath.startsWith("/"))) {
			rstPath = rstPath.substring(1, rstPath.length());
		}
		return rstPath;
	}

	private static String delEndSeparatorChar(String path) {
		String rstPath = path;
		while ((rstPath.length() > 1) && (rstPath.endsWith("/"))) {
			rstPath = rstPath.substring(0, rstPath.length() - 1);
		}
		return rstPath;
	}
	



}
